﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Collections;

namespace FireFly
{
    /// <summary>
    /// Base class for hierarchicaly organized view models
    /// </summary>
    public abstract class HierarchicalViewModelBase<TModel> : ViewModelBase<TModel>, IHierarchicalViewModel
    {
        IHierarchicalViewModel _parent;

        protected HierarchicalViewModelBase(TModel model) : base(model)
        {
        }

        public IHierarchicalViewModel Parent
        {
            get
            {
                return _parent;
            }
            set
            {
                _parent = value;
                OnPropertyChanged("Parent");
            }
        }

        public abstract ICollection VisualChildren
        {
            get;
        }
        
        public void RaiseVisualChildrenUpdated()
        {
            OnPropertyChanged("VisualChildren");
        }

        public override bool IsReadOnly
        {
            get
            {
                return Parent != null ? Parent.IsReadOnly : false;
            }
        }

        /// <summary>
        /// Remove this view model from its parents collection
        /// </summary>
        public virtual IRelayCommand RemoveCommand
        {
            get
            {
                if(_RemoveCommand == null)
                {
                    _RemoveCommand = new CommonRemoveItemCommand<HierarchicalViewModelBase<TModel>>(
                        "HierarchicalViewModelBase.RemoveCommand");
                }
                return _RemoveCommand;
            }
        }
        static IRelayCommand _RemoveCommand;
    }
}